// use std::{collections::HashMap, hash::Hash};

use std::collections::HashMap;


fn main1() {
    println!("Hello, world!");

    //  新建HashMap
    let mut scores = HashMap::new();
    // 插入元素
    scores.insert(String::from("Bule"), 10);
    scores.insert(String::from("Yello"), 20);
    // 访问元素值

    // let team_name = String::from("Blue");
    // let score = scores.get(&team_name).copied().unwrap_or(0);

    for (key, value) in &scores {
        println!("{key} : {value}");
    }

    // 哈希map和所有权（i32可以拷贝，String会失去所有权）
    let filed_name = String::from("Favorite color");
    let filed_value = String::from("Blue");

    let mut map = HashMap::new();
    map.insert(filed_name.clone(), filed_value);
    println!("{filed_name}");

    // 更新HashMap
    scores.insert(String::from("Blue"), 10);
    scores.insert(String::from("Blue"), 25);

    println!("{:?}", scores);

    // 只在键没有对应值时插入键值对
    scores.entry(String::from("Red")).or_insert(50);
    scores.entry(String::from("Blue")).or_insert(50);

    // 根据旧值更新一个值
    let text = "hello world wonderful world";

    let mut map = HashMap::new();
    for word in text.split_whitespace() {
        let count = map.entry(word).or_insert(0);
        *count += 1;
    }
    println!("{:?}", map);




}
